package com.mall.biz.user.record;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.mall.biz.user.AddressBizImpl;
import com.mall.client.item.TableNumService;
import com.mall.client.mall.MallService;
import com.mall.client.order.MallItemOrderService;
import com.mall.client.trade.TradeService;
import com.mall.client.user.UserLevelService;
import com.mall.client.user.UserService;
import com.mall.enums.AddressEnum;
import com.mall.pojo.Address;
import com.mall.pojo.MallItemOrder;
import com.mall.pojo.MallItemOrderExample;
import com.mall.pojo.MallItemOrderExample.Criteria;
import com.mall.pojo.Trade;
import com.mall.pojo.TradeExample;
import com.mall.pojo.User;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.item.MallItemOrderVO;
import com.mall.vo.item.MallItemVO;
import com.mall.vo.trade.TradeVO;


@Service
public class MallItemRecordBizImpl {
	@Autowired
	private MallItemOrderService mallItemOrderService;
	
	@Autowired
	private MallService mallService;
	
	@Autowired
	private TableNumService tableNumService;
	@Autowired
	private UserService userService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private UserLevelService userLevelService;
	@Autowired
	private AddressBizImpl addressBizImpl;

	
	/**
	 * 智慧餐厅订单列表
	 * @param tradeRecordQuery
	 * @return
	 */
	public List<MallItemOrderVO> getMallItemOrderByQuery(TradeRecordQuery tradeRecordQuery) {
		List<MallItemOrder>  mallItemOrderList  =  getMallItemOrderListByQuery(tradeRecordQuery);
		List<MallItemOrderVO> mallItemOrderVOList = new ArrayList<>();
		MallItemOrderVO mallItemOrderVO = null;
		MallItemOrder mallItemOrder = null;
		for (int i = 0; i < mallItemOrderList.size(); i++) {
			mallItemOrderVO = new MallItemOrderVO();
			mallItemOrder = mallItemOrderList.get(i);
			String mallItemJson = mallItemOrder.getBuyItemsJson();
			List<MallItemVO> mallItemVOList = null;
			if (mallItemJson != null) {
				 mallItemVOList =  JSONArray.parseArray(mallItemJson, MallItemVO.class);
			}
			
			mallItemOrderVO.setMallItemVOList(mallItemVOList);
			
			mallItemOrderVO.setMallItemOrder(mallItemOrder);;
			mallItemOrderVO.setMall(this.mallService.getMallById(mallItemOrder.getMallId()));
			mallItemOrderVO.setUser(this.userService.getUserByUserId(mallItemOrder.getBuyUserId()));
			mallItemOrderVO.setTableNum(this.tableNumService.getTableNumById(mallItemOrder.getTableNumId()));
			mallItemOrderVO.setTrade(this.tradeService.getTradeByParentId(mallItemOrder.getId()));
			mallItemOrderVOList.add(mallItemOrderVO);
		}
		if (mallItemOrderVOList != null && mallItemOrderVOList.size() > 0) {
			mallItemOrderVO = mallItemOrderVOList.get(0);
			}
		return mallItemOrderVOList;
	}

	
	/**
	 * mallItemOrder公用查询
	 * @param tradeRecordQuery
	 * @return
	 */
	private List<MallItemOrder> getMallItemOrderListByQuery(TradeRecordQuery tradeRecordQuery) {
		MallItemOrderExample example = new MallItemOrderExample();
		example.setOrderByClause("id desc");
		if(tradeRecordQuery.getPage() != null){
			example.setLimit(tradeRecordQuery.getPage().getLength());
			example.setStart(tradeRecordQuery.getPage().getBegin());
		}
		
		Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(tradeRecordQuery.getOnlyKey());
		if(tradeRecordQuery.getUserId() != null){
			criteria.andBuyUserIdEqualTo(tradeRecordQuery.getUserId());
		}
		if(tradeRecordQuery.getStatus() != null){
			criteria.andStatusEqualTo(tradeRecordQuery.getStatus());
		}
		if(tradeRecordQuery.getType() != null){
			criteria.andTypeEqualTo(tradeRecordQuery.getType());
		}
		List<MallItemOrder> mallItemOrderList = this.mallItemOrderService.getMallItemOrderListByExample(example);
		return mallItemOrderList;
	}


	public List<TradeVO> getTradeVOListByQuery(TradeRecordQuery tradeRecordQuery) {
		List<Trade> tradeList = getTradeListByQuery(tradeRecordQuery);
		
		List<TradeVO> tradeVOList = new ArrayList<>();
		TradeVO tradeVO = null;
		Trade trade = null;
		for (int i = 0; i < tradeList.size(); i++) {
			tradeVO = new TradeVO();
			trade = tradeList.get(i);
			MallItemOrder mallItemOrder =  this.mallItemOrderService.getMallItemOrderById(trade.getParentId());
			if (mallItemOrder != null) {
				String mallItemJson = mallItemOrder.getBuyItemsJson();
				List<MallItemVO> mallItemVOList = null;
				if (mallItemJson != null) {
					 mallItemVOList =  JSONArray.parseArray(mallItemJson, MallItemVO.class);
				}
				
				tradeVO.setMallItemVOList(mallItemVOList);
				
				//tradeVO.setMallItemOrder(mallItemOrder);;
				tradeVO.setMall(this.mallService.getMallById(trade.getMallId()));
				User user = this.userService.getUserByUserId(trade.getBuyUserId());
				tradeVO.setUser(user);
				if(user != null && user.getId()>0){
					tradeVO.setUserLevel(this.userLevelService.getUserLevelByLevelAndKey(user.getLevel().intValue(), tradeRecordQuery.getOnlyKey()));
					//tradeVO.setTableNum(this.tableNumService.getTableNumById(mallItemOrder.getTableNumId()));
					List<Address> addressList = this.addressBizImpl.getAddressListByUserAndstatus(user.getId(), tradeRecordQuery.getOnlyKey(), null, AddressEnum.DEFAULTADDRESS.getCode());
					Address address = this.addressBizImpl.getFirstAddress(addressList);
					tradeVO.setAddress(address);
				}
				tradeVO.setTrade(trade);
				tradeVOList.add(tradeVO);
			}
			
		}
		
		return tradeVOList;
		
	}
	
	private List<Trade> getTradeListByQuery(TradeRecordQuery tradeRecordQuery) {
		TradeExample example = new TradeExample();
		example.setOrderByClause("id desc");
		if(tradeRecordQuery.getPage() != null){
			example.setLimit(tradeRecordQuery.getPage().getLength());
			example.setStart(tradeRecordQuery.getPage().getBegin());
		}
		com.mall.pojo.TradeExample.Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(tradeRecordQuery.getOnlyKey());
		if(tradeRecordQuery.getUserId() != null){
			criteria.andBuyUserIdEqualTo(tradeRecordQuery.getUserId());
		}
		if(tradeRecordQuery.getStatus() != null){
			criteria.andStatusEqualTo(tradeRecordQuery.getStatus());
		}
		if(tradeRecordQuery.getType() != null){
			criteria.andTypeEqualTo(tradeRecordQuery.getType());
		}
		if(tradeRecordQuery.getRefund() != null){
			criteria.andRefundEqualTo(tradeRecordQuery.getRefund());
		}
		
		
		List<Integer> notInclude = new  ArrayList<>();
		notInclude.add(-1);
		notInclude.add(-2);
		criteria.andStatusNotIn(notInclude);
		List<Trade> tradeList = this.tradeService.getTradeMessListByExample(example);
		return tradeList;
		
	}
	

}
